package com.shizhanzhe.gxb.my.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.my.adapter.TiRecordAdapter;
import com.shizhanzhe.gxb.my.bean.TiBean;
import com.shizhanzhe.gxb.url.Path;
import com.shizhanzhe.gxb.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * @author YJ
 * @date 2019/1/4
 */
public class TiXianRecordActivity extends SwipeBackActivity {
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    @BindView(R.id.no_data)
    ImageView noData;
    private TiRecordAdapter mAdapter;
    private int page = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tixian_record);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarView(R.id.bar_0_view)
                .init();
        topTitle.setText("提现记录");
        initData();
        setListener();
    }

    @OnClick(R.id.backarea)
    public void onViewClicked() {
        finish();
    }

    private void initData() {
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TiRecordAdapter(TiXianRecordActivity.this);
        recycler.setAdapter(mAdapter);
        tiXianList(page);
    }
    private void setListener() {
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                tiXianList(page);
            }
        });
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                tiXianList( page);
            }
        });
    }

    private void tiXianList(int cPage) {
        OkHttpDownloadJsonUtil.downloadJson(TiXianRecordActivity.this, Path.txRecode(UserBean.uid, cPage), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    Gson gson = new Gson();
                    TiBean tiBean = gson.fromJson(json, TiBean.class);
                    if (tiBean.getData().getData().size() > 0) {
                        mAdapter.updateData(page,tiBean.getData().getData());
                    } else {
                        if (page == 1) {
                            srl.setVisibility(View.GONE);
                            noData.setVisibility(View.VISIBLE);
                        } else {
                            Toast.makeText(TiXianRecordActivity.this, "不能再有更多了", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

}
